package com.example.xunua_weather;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;

import com.example.xunua_weather.Adapter.CityManagerAdapter;
import com.example.xunua_weather.Sqliteutils.DBManager;
import com.example.xunua_weather.Sqliteutils.DatabaseBean;

import java.util.ArrayList;
import java.util.List;

public class CityManageActivity extends AppCompatActivity implements View.OnClickListener{
    ImageView backIv,deleteIv,addIv;
    ListView cityLv;
    private List<DatabaseBean> beanList;
    private CityManagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_manage);
        backIv=findViewById(R.id.city_back_iv);
        deleteIv=findViewById(R.id.city_delete_iv);
        addIv=findViewById(R.id.city_iv_add);
        cityLv=findViewById(R.id.city_lv);
        backIv.setOnClickListener(this);
        deleteIv.setOnClickListener(this);
        addIv.setOnClickListener(this);
        //获取数据库中所有城市的信息
        beanList=new ArrayList<>();
        //设置适配器
        adapter=new CityManagerAdapter(this,beanList);
        cityLv.setAdapter(adapter);
    }

//返回界面时，重新加载数据库信息，然后更新listview的列表的内容


    @Override
    protected void onResume() {
        super.onResume();
        List<DatabaseBean> list=DBManager.queryAllInfo();
        beanList.clear();
        beanList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.city_back_iv:
                finish();
                break;
            case R.id.city_delete_iv:
                Intent intent1 = new Intent(this, DeleteCityActivity.class);
                startActivity(intent1);
                break;
            case R.id.city_iv_add:
                int cityCount = DBManager.getCityCount();
                if (cityCount<5){
                    Intent intent = new Intent(this, SearchCityActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(this,"存储城市数量已达上限，请删除后再增加",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
